import {
  queryMyPermission,
  listEnterprisePermission,
  listEnterpriseProjectPermission,
  listRolePermission
} from '@/services/permision';

const filterMenuRoutes = (routes) => {
  console.log(routes)
  if (!routes){
    return;
  }
  routes = routes.filter(route => !route.hideInMenu)
  routes.forEach(route => {
    if(route.children){
      route.routes = filterMenuRoutes(route.routes)
    }
  })
  return routes;
}

const filterChildrenMenu = (menuItem, menus) => {
  if (menuItem.children) {
    const childrenMenuData = [];
    menuItem.children.forEach((item) => {
      if (menus[menuItem.path]) {
        const children = { ...item };
        childrenMenuData.children = filterChildrenMenu(children, menus)
        childrenMenuData.routes = childrenMenuData.children
        childrenMenuData.push(children);
      }
    })
    return childrenMenuData;
  }
}

const PermissionModel = {
  namespace: 'permission',
  state: {
    menuData: [],
  },
  effects: {
    *queryMyPermission({ payload }, { call, put }) {
      const response = yield call(queryMyPermission);
      if (response && response.success) {
        yield put({
          type: 'setUserMenu',
          payload: {
            routes: payload,
            permission: response.data
          },
        });
      }
    },
    *listEnterprisePermission({ payload }, { call, put }) {
      const response = yield call(listEnterprisePermission, payload);
      if (response && response.success) {
        yield put({
          type: 'setEnteprisePermission',
          payload: response.data,
        });
      }
    },
    *listEnterpriseProjectPermission({ payload }, { call, put }) {
      const response = yield call(listEnterpriseProjectPermission, payload);
      if (response && response.success) {
        yield put({
          type: 'setEntepriseProjectPermission',
          payload: response.data,
        });
      }
    },

    *listRolePermission({ payload }, { call, put }) {
      const response = yield call(listRolePermission, payload.roleId);
      if (response && response.success) {
        yield put({
          type: 'setRolePermission',
          payload: response.data,
        });
      }
    },
  },
  reducers: {
    setEnteprisePermission(state, action) {
      return { ...state, enteprisePermission: action.payload }
    },
    setEntepriseProjectPermission(state, action) {
      return { ...state, entepriseProjectPermission: action.payload }
    },
    setRolePermission(state, action) {
      return { ...state, rolePermission: action.payload }
    },

    setUserMenu(state, action) {
      const menus = action.payload.permission.menu || [];
      const menuData = [];
      let routes = action.payload.routes;
      routes.forEach(element => {
        if (menus[element.path]) {
          const menuItem = { ...element };
          menuItem.children = filterChildrenMenu(menuItem, menus)
          menuItem.routes = menuItem.children
          menuData.push(menuItem);
        }
      })
      // return { ...state, menuData: menuData || [] };
      return { ...state, menuData: routes || [] };
    },
  },
};
export default PermissionModel;
